<?php

namespace App\Repositories;


use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;

//引入hash

class VipRepository
{

    /*vip特权列表*/
    public function getPrivilegeList($page, $perPage)
    {
        $list = app("model_grade_privilege")
            ->selectRaw('*')
            ->orderBy("id", "desc")
            ->paginate($perPage, ['*'], '', $page);

        return $list;
    }

    /*新增特权*/
    public function privilegeAdd($param)
    {
        $isHave = app("model_grade_privilege")->where(['type' => $param['type']])->first();
        if ($isHave) {
            throw new BasicException(93016);
        }
        $addRes = app("model_grade_privilege")->insert([
            'name' => $param['name'],
            'status' => $param['status'],
            'type' => $param['type'],
            'icon_img' => $param['icon_img'],
            'dark_icon_img' => $param['dark_icon_img'],
            'explain_img' => $param['explain_img'],
            'explain1' => $param['explain1'],
            'explain2' => $param['explain2'],
            'sort' => $param['sort'],
        ]);

        return $addRes;
    }

    /*编辑特权数据*/
    public function privilegeEditDetail($param)
    {
        $gradePrivilege = app("model_grade_privilege")
            ->selectRaw('*')
            ->where(['id' => $param['id']])
            ->first();

        return $gradePrivilege;
    }

    /*编辑特权*/
    public function privilegeEdit($param)
    {
        $gradePrivilege = app("model_grade_privilege")->where(['id' => $param['id']])->first();
        if (!$gradePrivilege) {
            throw new BasicException(93017);
        }

        $saveRes = app("model_grade_privilege")->where('id', $param['id'])->update([
            'name' => $param['name'],
            'status' => $param['status'],
            'type' => $param['type'],
            'icon_img' => $param['icon_img'],
            'dark_icon_img' => $param['dark_icon_img'],
            'explain_img' => $param['explain_img'],
            'explain1' => $param['explain1'],
            'explain2' => $param['explain2'],
            'sort' => $param['sort'],
        ]);

        return $saveRes;
    }

    /*vip大等级列表*/
    public function gradeBigList($page, $perPage)
    {
        $list = app("model_grade_big")
            ->selectRaw('*')
            ->orderBy("id", "desc")
            ->paginate($perPage, ['*'], '', $page);
        foreach ($list as $item) {
            $gradePrivilegeRes = [];
            if ($item->privilege_id) {
                $privilege_id = explode(',', $item->privilege_id);
                $gradePrivilegeRes = app("model_grade_privilege")->select('id', 'name')->where(['status' => 1])->whereIn('id', $privilege_id)->get()->toArray();
            }
            $item->privilege_data = $gradePrivilegeRes;
            unset($item->privilege_id);
        }

        return $list;
    }

    /*新增vip大等级特权列表*/
    public function gradeBigPrivilegeList($page, $perPage)
    {
        $list = app("model_grade_privilege")
            ->selectRaw('id,name')
            ->where(['status' => 1])
            ->orderBy("id", "desc")
            ->get();

        return $list;
    }

    /*新增vip大等级*/
    public function gradeBigAdd($param)
    {
        $privilege_id = json_decode($param['privilege_id']);
        if (!$privilege_id) {
            throw new BasicException(93018);
        }
        $privilege_id = app("model_grade_privilege")->where(['status' => 1])->whereIn('id', $privilege_id)->pluck('id')->toArray();
        if (!$privilege_id) {
            throw new BasicException(93018);
        }
        $isHave = app("model_grade_big")->where(['grade' => $param['grade']])->first();
        if ($isHave) {
            throw new BasicException(93019);
        }

        $addRes = app("model_grade_big")->insert([
            'grade' => $param['grade'],
            'name' => $param['name'],
            'big_img' => $param['big_img'],
            'privilege_id' => implode(',', $privilege_id),
        ]);

        return $addRes;
    }

    /*编辑vip大等级数据*/
    public function gradeBigEditDetail($param)
    {
        $gradePrivilege = app("model_grade_big")
            ->selectRaw('*')
            ->where(['id' => $param['id']])
            ->first();
        $gradePrivilege['privilege_id'] = explode(',', $gradePrivilege['privilege_id']);

        return $gradePrivilege;
    }

    /*编辑vip大等级*/
    public function gradeBigEdit($param)
    {
        $privilege_id = json_decode($param['privilege_id']);
        if (!$privilege_id) {
            throw new BasicException(93018);
        }
        $privilege_id = app("model_grade_privilege")->where(['status' => 1])->whereIn('id', $privilege_id)->pluck('id')->toArray();
        if (!$privilege_id) {
            throw new BasicException(93018);
        }
        $gradeBig = app("model_grade_big")->where(['id' => $param['id']])->first();
        if (!$gradeBig) {
            throw new BasicException(93420);
        }

        $saveRes = app("model_grade_big")->where('id', $param['id'])->update([
            'grade' => $param['grade'],
            'name' => $param['name'],
            'big_img' => $param['big_img'],
            'privilege_id' => implode(',', $privilege_id),
        ]);

        return $saveRes;
    }

    public function gradeBigDel($param)
    {
        $gradeBig = app("model_grade_big")->where(['id' => $param['id']])->first();
        if (!$gradeBig) {
            throw new BasicException(93420);
        }

        DB::beginTransaction();
        try {
            $rankDel = app('model_grade_big')->where('id', $param['id'])->delete();
            if (!$rankDel) {
                throw new BasicException(93012);
            }

            $gradeConfigHave = app('model_grade_config')->where('grade_big_id', $param['id'])->first();
            if ($gradeConfigHave) {
                $rankDel = app('model_grade_config')->where('grade_big_id', $param['id'])->delete();
                if (!$rankDel) {
                    throw new BasicException(93012);
                }
            }

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new BasicException($e->getCode());
        }

        return true;
    }
}
